# CMakeLists.txt for libgnucash/core-utils

add_subdirectory(test)


### libgnc-core-utils
set (core_utils_SOURCES
  binreloc.c
  gnc-prefs.c
  gnc-environment.c
  gnc-filepath-utils.cpp
  gnc-gkeyfile-utils.c
  gnc-glib-utils.c
  gnc-jalali.c
  gnc-locale-utils.c
  gnc-locale-utils.cpp
  gnc-path.c
  gnc-version.c
)

# Add dependency on config.h
set_source_files_properties (${core_utils_SOURCES} PROPERTIES OBJECT_DEPENDS ${CONFIG_H})

set(core_utils_noinst_HEADERS
  binreloc.h
  gnc-prefs.h
  gnc-prefs-p.h
  gnc-environment.h
  gnc-filepath-utils.h
  gnc-gkeyfile-utils.h
  gnc-glib-utils.h
  gnc-jalali.h
  gnc-locale-utils.h
  gnc-locale-utils.hpp
  gnc-path.h
  gnc-version.h
)

add_library(gnc-core-utils ${core_utils_SOURCES} ${core_utils_noinst_HEADERS})

add_dependencies(gnc-core-utils gnc-vcs-info)

target_include_directories(gnc-core-utils
    PUBLIC
        ${CMAKE_SOURCE_DIR}/common
        ${CMAKE_BINARY_DIR}/common
        ${CMAKE_CURRENT_BINARY_DIR}
        ${CMAKE_CURRENT_SOURCE_DIR}
        ${GLIB2_INCLUDE_DIRS}
    PRIVATE
        ${GTK_MAC_INCLUDE_DIRS})

target_link_libraries(gnc-core-utils
    PRIVATE
        ${Boost_LIBRARIES}
        ${GLIB2_LDFLAGS}
        ${GOBJECT_LDFLAGS}
        ${GTK_MAC_LDFLAGS}
        "$<$<BOOL:${MAC_INTEGRATION}>:${OSX_EXTRA_LIBRARIES}>")

target_compile_definitions(gnc-core-utils
    PRIVATE
        G_LOG_DOMAIN=\"gnc.core-utils\"
        $<$<BOOL:${BUILDING_FROM_VCS}>:GNC_VCS=\"git\">
        ${GTK_MAC_CFLAGS_OTHER})

target_compile_options(gnc-core-utils
    PRIVATE
        $<$<BOOL:${MAC_INTEGRATION}>:${OSX_EXTRA_COMPILE_FLAGS}>)

install(TARGETS gnc-core-utils
  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)


### gncla-dir.h
set(prefix ${CMAKE_INSTALL_PREFIX})
set(datadir ${CMAKE_INSTALL_FULL_DATADIR})
set(bindir ${CMAKE_INSTALL_FULL_BINDIR})
set(libdir ${CMAKE_INSTALL_FULL_LIBDIR})
set(sysconfdir ${CMAKE_INSTALL_FULL_SYSCONFDIR})
set(localedir "${CMAKE_INSTALL_FULL_DATAROOTDIR}/locale")
configure_file(gncla-dir.h.in gncla-dir.h)


### Create gnc-vcs-info.h
# This can only be done when building from a vcs (git/svn/bzr/svk) working directory.
# This file is shipped in the distribution tarball, so no need to generate it in that case anyway.
# The meta-cmake gymnastics here come from https://cmake.org/pipermail/cmake/2010-July/038015.html
if (BUILDING_FROM_VCS)
  set(VCS_INFO_BASE_DIR ${CMAKE_BINARY_DIR})
else()
  set(VCS_INFO_BASE_DIR ${CMAKE_SOURCE_DIR})
endif()
set(VCS_INFO_FILE ${VCS_INFO_BASE_DIR}/libgnucash/core-utils/gnc-vcs-info.h CACHE STRING "path to gnc-vcs-info.h file")

if (BUILDING_FROM_VCS)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/gnc-vcs-info.h.in
"/* Autogenerated. Do not change. */
#define GNC_VCS_REV \"@GNC_VCS_REV@\"
#define GNC_VCS_REV_DATE \"@GNC_VCS_REV_DATE@\"
#define GNC_VCS_REV_YEAR \"@GNC_VCS_REV_YEAR@\"
#define GNC_VCS_REV_Y_M \"@GNC_VCS_REV_Y_M@\"
")

add_custom_target(gnc-vcs-info ALL
    BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/gnc-vcs-info.h
    COMMAND ${CMAKE_COMMAND} -D SRC=${CMAKE_CURRENT_BINARY_DIR}/gnc-vcs-info.h.in
                             -D DST=${CMAKE_CURRENT_BINARY_DIR}/gnc-vcs-info.h
                             -D SRC_DIR=${CMAKE_SOURCE_DIR}
                             -D SHELL="${SHELL}"
                             -P ${CMAKE_SOURCE_DIR}/cmake/git2version-info.cmake
)
else(BUILDING_FROM_VCS)
  add_custom_target(gnc-vcs-info DEPENDS ${VCS_INFO_FILE})
endif(BUILDING_FROM_VCS)
dist_add_generated (${BUILDING_FROM_VCS} gnc-vcs-info.h)

# Dist files
set_local_dist(core_utils_DIST_local
    ${core_utils_SOURCES}
    ${core_utils_noinst_HEADERS}
    CMakeLists.txt
    gncla-dir.h.in)
set(core_utils_DIST ${core_utils_DIST_local} ${test_core_utils_DIST} PARENT_SCOPE)
